﻿Public Class frmAddStaffs
    Dim VStaffID, VStaffTypeID, VDepartmentID, VLocationID, VPositionID, VSalarycodeID As Integer
    Dim StaffImg As String = "defualtImage.jpg"
    Dim FN As String

    Private Sub frmAddStaffs_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Connect()
        Me.TabPStaffDetail.Enabled = False
        Me.TabPFamily.Enabled = False
        Me.txtStaffdetailID.Text = AutoID4Access("SELECT StaffdetailID FROM tbl_staffdetail", "tbl_staffdetail") + 1
        Me.txtID.Text = AutoID4Access("SELECT StaffID FROM tbl_staffs", "tbl_staff") + 1

    End Sub
    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        Me.TabPStaffDetail.Enabled = True
        Me.cboStaffType.Items.Clear()
        LoadDataToCombo("SELECT * FROM tbl_stafftypes", cboStaffType)
        LoadDataToCombo("SELECT * FROM tbl_departments", CboDepartment)
        LoadDataToCombo("SELECT * FROM tbl_location", cboLocation)
        'Load1FieldToCombo("SELECT * FROM tbl_location", cboLocation)
        LoadDataToCombo("SELECT * FROM tbl_positions", CboPosition)
        LoadDataToCombo("SELECT * FROM tbl_salarycode", cboSalaryCode)

        Dim Cont As String
        Cont = Me.CboVillage.Text & "," & Me.CboCommunce.Text & "," & cboDistrict.Text & "," & CboProvince.Text

        Dim SQLSave As String
        If txtFamilyName.Text = "" Then
            FN = "N/A"
        Else
            FN = txtFamilyName.Text
        End If
        StaffImg = "defualtImage.jpg"
        'SQLSave = "INSERT INTO tbl_staffs(StaffID,KName,LName,DOB,[Gender],[Village],[Communce],[District],[Province],[Contact],UrgentContact,[Tel],[Email],FamilyStatus,FamilyName,StaffImage)" & _
        '                    "VALUES(" & txtID.Text & ",'" & txtName.Text & "','" & txtLName.Text & "','" & FormatDateTime(txtDOB.Text, DateFormat.ShortDate) & "','" & CboGender.Text & "','" & CboVillage.Text & _
        '                    "','" & CboCommunce.Text & "','" & cboDistrict.Text & "','" & CboProvince.Text & "','" & txtUrgentContact.Text & "','" & txtTel.Text & _
        '                    "','" & txtEmail.Text & "','" & cboFamilyStatus.Text & "','" & txtFamilyName.Text & "','" & StaffImg & "')"
        SQLSave = "INSERT INTO tbl_staffs(StaffID,KName,LName,[DOB],[Gender],[Village],[Communce],[District],[Province],[Contact],[UrgentContact],[Tel],[Email],[FamilyStatus],[FamilyName],Img)" & _
                        "VALUES(" & txtID.Text & ",'" & txtName.Text & "','" & txtLName.Text & "','" & FormatDateTime(txtDOB.Text, DateFormat.ShortDate) & "','" & CboGender.Text & "','" & CboVillage.Text & _
                        "','" & CboCommunce.Text & "','" & cboDistrict.Text & "','" & CboProvince.Text & "','" & Cont & "','" & txtUrgentContact.Text & "','" & txtTel.Text & "','" & txtEmail.Text & "','" & cboFamilyStatus.Text & _
                        "','" & FN & "','" & StaffImg & "')"

        SaveDataToAccess(SQLSave)
        'ClearText()
        'Me.txtID.Text = AutoID4Access("SELECT StaffID FROM tbl_staffs", "tbl_staff") + 1
        VStaffID = txtID.Text

    End Sub

    Sub ClearText()
        Me.txtID.Text = ""
        Me.txtStaffdetailID.Text = ""
        Me.txtName.Text = ""
        Me.txtChDOB.Text = ""
        Me.txtChID.Text = ""
        Me.txtChKName.Text = ""
        Me.txtChLName.Text = ""
        Me.txtDOB.Text = Now
        Me.txtEmail.Text = ""
        Me.txtFamilyName.Text = ""
        Me.txtTel.Text = ""
        Me.txtLName.Text = ""
        Me.txtHouse.Text = ""

        Me.txtUrgentContact.Text = ""
        Me.CboChGender.Text = ""
        Me.CboCommunce.Text = ""
        Me.cboDistrict.Text = ""
        Me.cboFamilyStatus.Text = ""
        Me.CboGender.Text = ""
        Me.CboProvince.Text = ""
        Me.CboVillage.Text = ""

        VStaffID = 0
        VStaffTypeID = 0
        VDepartmentID = 0
        VLocationID = 0
        VPositionID = 0
        VSalarycodeID = 0
        Me.txtID.Text = AutoID4Access("SELECT StaffID FROM tbl_staffs", "tbl_staff") + 1
    End Sub

    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        ClearText()

    End Sub

    Private Sub cboFamilyStatus_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboFamilyStatus.SelectedIndexChanged
        If cboFamilyStatus.Text <> "នៅលីវ" Then
            Me.lblFamily.Visible = True
            Me.txtFamilyName.Visible = True
        Else
            Me.lblFamily.Visible = False
            Me.txtFamilyName.Visible = False
        End If

    End Sub
    Private Sub cboLocation_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboLocation.SelectedIndexChanged
        'MsgBox("Location ID: " & Me.cboLocation.Text.Substring(0, Me.cboLocation.Text.LastIndexOf("-")))
        VLocationID = 0
        VLocationID = cboLocation.Text.Substring(0, cboLocation.Text.LastIndexOf("-"))

    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click

    End Sub

    Private Sub cboStaffType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboStaffType.SelectedIndexChanged
        VStaffTypeID = 0
        VStaffTypeID = cboStaffType.Text.Substring(0, cboStaffType.Text.IndexOf("-"))

    End Sub

    Private Sub CboDepartment_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboDepartment.SelectedIndexChanged
        VDepartmentID = 0
        VDepartmentID = GetID(Me.CboDepartment.Text)
    End Sub

    Private Sub CboPosition_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboPosition.SelectedIndexChanged
        VPositionID = 0
        VPositionID = GetID(CboPosition.Text)

    End Sub

    Private Sub cboSalaryCode_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboSalaryCode.SelectedIndexChanged
        VSalarycodeID = 0
        VSalarycodeID = GetID(cboSalaryCode.Text)
    End Sub

    Private Sub btnNext2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext2.Click
        Me.TabPFamily.Enabled = True
        Dim SQLSave As String

        SQLSave = "INSERT INTO tbl_staffdetail(StaffdetailID,StaffID,StaffTypeID,DepartmentID,LocationID,PositionID,SalaryCodeID,StaffCode,AccountCode,[Salary],[GeneralQalification],[Certificate],[Skill],[Training],[Languages],[Level])" & _
                       "VALUES(" & txtStaffdetailID.Text & "," & VStaffID & "," & VStaffTypeID & "," & VDepartmentID & "," & VLocationID & "," & VPositionID & _
                       "," & VSalarycodeID & ",'" & txtCodeStaff.Text & "','" & txtAccountCode.Text & "'," & txtSalary.Text & ",'" & cboGenerQualification.Text & _
                       "','" & cboCertificate.Text & "','" & txtSkill.Text & "','" & txtTraining.Text & "','" & txtLanguage.Text & "','" & txtLevel.Text & "')"

        'SQLSave = "INSERT INTO tbl_staffdetail(StaffdetailID,StaffID,StaffTypeID,DepartmentID,LocationID,PositionID,SalaryCodeID,StaffCode)" & _
        '              "VALUES(" & txtStaffdetailID.Text & "," & VStaffID & "," & VStaffTypeID & "," & VDepartmentID & "," & VLocationID & "," & VPositionID & _
        '              "," & VSalarycodeID & ",'" & txtCodeStaff.Text & "')" ','" & txtAccountCode.Text & "'," & txtSalary.Text & ",'" & cboGenerQualification.Text & _
        '              "','" & cboCertificate.Text & "','" & txtSkill.Text & "','" & txtTraining.Text & "','" & txtLanguage.Text & "','" & txtLevel.Text & "')"

        'SQLSave = "INSERT INTO tbl_staffdetail(StaffdetailID,StaffID,StaffTypeID,DepartmentID,LocationID,PositionID,SalaryCodeID,StaffCode,AccountCode,[Salary],[GeneralQalification],[Certificate],[Skill],[Training],[Languages],[Level])" & _
        '              "VALUES(" & txtStaffdetailID.Text & "," & VStaffID & "," & VStaffTypeID & "," & VDepartmentID & "," & VLocationID & "," & VPositionID & _
        '              "," & VSalarycodeID & ",'" & txtCodeStaff.Text & "','" & txtAccountCode.Text & "'," & txtSalary.Text & ",'" & cboGenerQualification.Text & _
        '              "','" & cboCertificate.Text & "','" & txtSkill.Text & "','" & txtTraining.Text & "','" & txtLanguage.Text & "','" & txtLevel.Text & "')"


        SaveDataToAccess(SQLSave)

    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim SQLSave As String
        Dim ChID As String = AutoID4Access("SELECT ChildID FROM tbl_children", "tbl_children") + 1

        txtChID.Text = ChID

        SQLSave = "INSERT INTO tbl_children(ChildID,StaffID,KChildName,EChildName,ChildGender,ChildDOB)" & _
                       "VALUES(" & txtChID.Text & "," & VStaffID & ",'" & txtChKName.Text & "','" & txtChLName.Text & "','" & CboChGender.Text & "','" & txtChDOB.Text & "')"
        SaveDataToAccess(SQLSave)
        LoadDataToLV("SELECT ChildID,KChildName,EChildName,ChildGender,ChildDOB FROM tbl_children WHERE StaffID=" & txtStaffID.Text, ListView1)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.TabPFamily.Enabled = True
        Me.TabPStaffDetail.Enabled = True



    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        LoadDataToLV("SELECT ChildID,KChildName,EChildName,ChildGender,ChildDOB FROM tbl_children WHERE StaffID=" & txtStaffID.Text, ListView1)
    End Sub
End Class